package com.example.config;

import org.junit.Test;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.net.URL;

/**
 * <p>
 * swagger文档配置
 * </p>
 *
 * @author MrWen
 **/
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
@Configuration
//@EnableSwagger2 //旧版本 登录方式(http://localhost:9988/swagger-ui.html)
@EnableSwagger2WebMvc//新版本 登录方式(http://localhost:9988/doc.html) 里面默认有文档的下载
public class SwaggerConfig {

    @Bean
    public Docket buildDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(buildApiInf())
                .select()
                //controller包路径
                .apis(RequestHandlerSelectors.basePackage("com.example.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 描述信息
     */
    private ApiInfo buildApiInf() {
        return new ApiInfoBuilder()
                .title("swagger测试")
                .version("1.0")
                .description("swagger测试描述")
                .termsOfServiceUrl("http://www.example.com")
                .build();
    }

}
